﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Text.RegularExpressions;
using System.IO;
using System.Collections.Specialized;

namespace HTML2TEXT
{
    public partial class HTML2TEXT : Form
    {
        public HTML2TEXT()
        {
            InitializeComponent();

        }
        public string GetHTMLFromURL(string url)
        {

            string html = "";
            try
            {
                if (url != "")
                {
                    HttpWebRequest request = (System.Net.HttpWebRequest)HttpWebRequest.Create(url);

                    HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
                    // get the response stream.
                    System.IO.Stream responseStream = response.GetResponseStream();
                    // use a stream reader that understands UTF8
                    System.IO.StreamReader reader = new System.IO.StreamReader(responseStream, System.Text.Encoding.UTF8);
                    html = reader.ReadToEnd(); response.Close();
                    reader.Close();
                }
                else MessageBox.Show("Vui lòng nhập vào Đường dẫn (URL)");
            }
            catch { MessageBox.Show("Địa chỉ ULR không tồn tại"); }
            finally
            {

            }
            return html;
        }
        public String ConvertHTML2TEXT(string text)
        {
            string temp = "";
            temp = Regex.Replace(text, "\n", string.Empty);
            temp = Regex.Replace(temp, "<script type=\"text/javascript\">.*?</script>", string.Empty);
            temp = Regex.Replace(temp, "<.*?>", string.Empty);
            temp = Regex.Replace(temp, "&.*?;", string.Empty);
            return temp;
        }
        public void AlignText(RichTextBox a)
        {
            a.Text = a.Text.Trim();
            a.Text = a.Text.Replace("\n", "");

        }

        private void btn_GetHTML_Click(object sender, EventArgs e)
        {
            rtb_Text.Text = GetHTMLFromURL(txt_URL.Text);
            // AlignText(rtb_Text);
        }

        private void btn_GetAndConver_Click(object sender, EventArgs e)
        {
            rtb_Text.Text = ConvertHTML2TEXT(GetHTMLFromURL(txt_URL.Text));
            AlignText(rtb_Text);
        }

        private void btn_HTML2TEXT_Click(object sender, EventArgs e)
        {

            rtb_Text.Text = ConvertHTML2TEXT(rtb_Text.Text);
            AlignText(rtb_Text);
        }
        private void btn_Load_Click(object sender, EventArgs e)
        {

            rtb_Text.Text = ConvertHTML2TEXT(Readfile()); AlignText(rtb_Text);

        }
        private void btn_Save_Click(object sender, EventArgs e)
        {
            try
            {
                SaveFileDialog saveFileDlg = new SaveFileDialog();
                saveFileDlg.InitialDirectory = Application.ExecutablePath;
                saveFileDlg.Filter = "Lưu thành file Text (*.txt)|*.txt";
                if (saveFileDlg.ShowDialog() == DialogResult.OK)
                {
                    FileStream fs = new FileStream(saveFileDlg.FileName, FileMode.Create);
                    StreamWriter sw = new StreamWriter(fs);
                    sw.Write(rtb_Text.Text.ToString());
                    sw.Flush();
                    sw.Close();
                    fs.Close();

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "");
            }
        }
        private StringCollection ReadFileIntoStringCollection(string file)
        {
            const int MaxBytes = 1000000;
            StreamReader sr = new StreamReader(file);
            StringCollection result = new StringCollection();
            int nBytesRead = 0;
            string nextLine;
            while ((nextLine = sr.ReadLine()) != null)
            {
                nBytesRead += nextLine.Length;
                if (nBytesRead > MaxBytes)
                    break;
                result.Add(nextLine);
            }
            sr.Close();
            return result;
        }
       
        private string Readfile()
        {
            string temp = "";
            OpenFileDialog opFile = new OpenFileDialog();
            opFile.InitialDirectory = Application.ExecutablePath;

            opFile.Filter = "Web (*html)|*html|Text(*txt)|*txt|Tất cả File(*.*)|*";
            if (opFile.ShowDialog() == DialogResult.OK)
            {
                FileStream fs = new FileStream(opFile.FileName, FileMode.Open);
                StreamReader readfile = new StreamReader(fs);
                temp = readfile.ReadToEnd();
                readfile.Close();
                readfile.Dispose();
            }
            return temp;
        }
    }
}
